package com.bear.newsset.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bear.newsset.R;
import com.bear.newsset.adapter.BaseXRecyclerViewAdapter;
import com.bear.newsset.adapter.OptionAdapter;
import com.bear.newsset.entity.OptEntity;
import com.bear.newsset.mvp.presen.MePresen;
import com.bear.newsset.mvp.view.MeView;
import com.bear.newsset.util.DOM;
import com.bear.newsset.util.LogUtils;
import com.bear.newsset.widget.FixedGridView;
import com.bear.newsset.widget.SimpleUserInfoView;
import com.google.android.material.appbar.AppBarLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.ybear.ybmvp.annotations.BindView;
import com.ybear.ybmvp.annotations.Presen;
import com.ybear.ybmvp.view.v4.YBFragment;

import java.util.List;

import static com.bear.newsset.util.Constant.DOM_ID_MENU_VIEW_CHECKED_POSITION;
import static com.bear.newsset.util.Constant.DOM_ID_NEWS_MANAGE_ACTIVITY_DESTROY;

/**
 * 我的页面
 */
public class MeFragment extends YBFragment
        implements MeView, AppBarLayout.OnOffsetChangedListener, DOM.OnResultListener {
    @Presen
    private MePresen mPresen;

    @BindView(R.id.fragment_me_coordinator_layout)
    private CoordinatorLayout lCoordinator;

    @BindView(R.id.fragment_me_appbar_layout)
    private AppBarLayout lAppBar;

    @BindView(R.id.fragment_me_sui_view)
    private SimpleUserInfoView suiView;

    @BindView(R.id.fragment_me_fgv_action_grid)
    private FixedGridView fgvActionView;

    @BindView(R.id.fragment_me_xrv_action_list)
    private XRecyclerView xrvActionListView;

    private OptionAdapter mOptAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_me, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        DOM.getInstance().registerResult( this );
        initData();
        initListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        //AppBar滑动监听器
        lAppBar.addOnOffsetChangedListener( this );
    }

    @Override
    public void onPause() {
        super.onPause();
        //移除AppBar滑动监听器
        lAppBar.removeOnOffsetChangedListener( this );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DOM.getInstance().unRegisterResult( this );
    }

    private void initData() {
        /* 数据源 */
        mPresen.setGridActionData();
        List<OptEntity> optData = mPresen.getOptData();

        //布局排列方式
        xrvActionListView.setLayoutManager( new LinearLayoutManager( getContext() ) );
        //禁止下拉刷新
        xrvActionListView.setPullRefreshEnabled( false );
        //禁止加载更多
        xrvActionListView.setLoadingMoreEnabled( false );

        //Opt适配器
        mOptAdapter = new OptionAdapter( optData );
        //设置List适配器
        xrvActionListView.setAdapter( mOptAdapter );
        //设置Grid数据
        fgvActionView.setData( mPresen.getGridActionData() );
    }

    private void initListener() {
        //Grid Item点击事件监听器
        fgvActionView.setOnItemClickListener((parent, view, position, id) -> {
            if( position == 4 ) {
                Toast.makeText(getContext(), "尚未开放", Toast.LENGTH_SHORT).show();
            }else {
                //打开新闻管理页面
                mPresen.startNewsManageActivity( position );
            }
        });

        //Opt Item点击事件监听器
        mOptAdapter.setOnItemClickListener(new BaseXRecyclerViewAdapter.OnItemClickListener<OptEntity>() {
            @Override
            public void onItemClick(View view, OptEntity itemData, int position) {
                Toast.makeText(view.getContext(), "尚未开放", Toast.LENGTH_SHORT).show();
            }
        });

        //用户信息View滑动状态事件监听器
        suiView.setOnScrollStatusListener((v, isUp) -> {
            /* 上滑时设置渐变背景，下滑时恢复 */
            int whiteRes = R.color.colorWhite;
            //设置根布局背景
            lCoordinator.setBackgroundResource(isUp ? R.color.colorChild : whiteRes);
            //设置当前View的背景
            v.setBackgroundResource(isUp ? R.drawable.shape_me_user_bg : whiteRes);
            //设置Grid的背景
            fgvActionView.setBackgroundResource(isUp ? R.drawable.shape_me_gv_action_bg : whiteRes);
        });
    }

    /**
     * AppBar滑动监听器
     * @param appBarLayout      AppBar
     * @param i                 滑动的y轴
     */
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        //触发滑动事件
        suiView.onScrollListener( i, appBarLayout.getTotalScrollRange());
    }

    @Override
    public void onResult(int id, Object data) {
        int i = -1;
        //切换菜单或者新闻页面关闭时刷新
        if( id == DOM_ID_MENU_VIEW_CHECKED_POSITION ) i = (int) data;
        if( id == DOM_ID_NEWS_MANAGE_ACTIVITY_DESTROY ) i = 1;
        if( i == 1 ) {
            mPresen.updateGridActionCount();
            fgvActionView.updateData( mPresen.getGridActionData() );
            LogUtils.d("MeFragment", "updateGridActionCount");
        }
    }
}
